#include "TEight.h"
#include <iostream>
#include <fstream>
using namespace std;
int TEight::total = 0;
int TEight::dir[]={1,-3,-1,3};
int TEight::target[Num+1];

bool TEight::operator==(const TEight &teight)//判断两个状态是否相同
{
	for (int i = 1; i <= Num;i++)
		if (teight.origin[i] != origin[i])
			return false;
	return true;
}
bool TEight::extend(int direction)//扩展
{
	if (( direction == RIGHT && space % 3 == 0 )
			|| ( direction == UP && space <= 3 )
			|| ( direction == LEFT && space % 3 == 1 )
			|| ( direction == DOWN && space > 6))
		return false;
	int temp = space;
	lastSpace = space;
	space += dir[direction];
	origin[temp] = origin[space];
	origin[space] = 0;
	return true;
}
bool TEight::isFind()
{
	for(int i=1;i<=Num;i++)
		{
			if(origin[i] != target[i])
				return false;
		}

	return true;
}
void TEight::printSelf()//把路径打印到结果文件
{
	ofstream ofile("output.txt",ios::app);
	if(total==0)
	{
		cout<<"初始姿态："<<endl;
		for (int i = 1; i <= Num;i++)
		{
			cout << " " << origin[i];
			if(i%3==0)
				cout<<endl;
		}

		ofile<<"初始姿态："<<endl;
		for (int i = 1; i <= Num;i++)
		{
			ofile << " " << origin[i];
			if(i%3==0)
				ofile<<endl;
		}
		total++;
	}
	else
	{
		cout << "第"<<total << "步："<<endl;;
		for (int i = 1; i <= Num;i++)
		{
			cout << " " << origin[i];
			if(i%3==0)
				cout<<endl;
		}

		ofile << "第"<<total << "步："<<endl;;
		for (int i = 1; i <= Num;i++)
		{
			ofile << " " << origin[i];
			if(i%3==0)
				ofile<<endl;
		}
		total++;
	}
	cout << endl;
	ofile << endl;
}

